<template>
    <div>
        <!-- 导航部分开始 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页 </el-breadcrumb-item>>
            <el-breadcrumb-item>质量管理</el-breadcrumb-item>
            <el-breadcrumb-item>常见缺陷</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 导航部分结束 -->

        <!--  搜索栏开始 -->
        <el-form style="margin-top: 15px;" :inline="true" :model="queryDefect" class="demo-form-inline">
            <el-form-item label="缺陷描述">
                <el-input v-model="queryDefect.defectName" placeholder="缺陷描述" clearable />
            </el-form-item>


            <el-form-item label="检测项类型">
                <el-select v-model="queryDefect.defectType" placeholder="请选择检测项类型">
                    <el-option label="尺寸" value="尺寸" />
                    <el-option label="外观" value="外观" />
                    <el-option label="重量" value="重量" />
                    <el-option label="性能" value="性能" />
                    <el-option label="成分" value="成分" />
                </el-select>
            </el-form-item>


            <el-form-item label="缺陷等级">
                <el-select v-model="queryDefect.defectLevel" placeholder="请选择缺陷等级">
                    <el-option label="致命缺陷" value="致命缺陷" />
                    <el-option label="严重缺陷" value="严重缺陷" />
                    <el-option label="轻微缺陷" value="轻微缺陷" />
                </el-select>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="loaddefect">搜索</el-button>
                <el-button @click="resettingdefect">重置</el-button>

            </el-form-item>
        </el-form>
        <!--  搜索栏结束 -->

        <!-- 工具栏开始 -->
        <div class="flex">
            <el-button title="新增" @click="this.addDefectDialogVisible = true" type="primary"><el-icon>
                    <Plus />
                </el-icon>新增</el-button>
            <el-button color="#b3e19d" @click="handlerEditDefect" title="修改" type="primary"><el-icon>
                    <EditPen />
                </el-icon>修改</el-button>

            <el-button color="#f89898" @click="handlerDeleteDefect" title="删除" type="primary"><el-icon>
                    <Delete />
                </el-icon>删除</el-button>
        </div>
        <!-- 工具栏结束 -->
        <!-- 表格 开始 -->
        <el-table ref="defectTable" :data="defects" max-height="800px" :stripe="true" :border="true"
            style="width: 90%;margin-top: 10px;">
            <el-table-column type="selection" width="55" />

            <el-table-column label="缺陷描述" property="defectName" />
            <el-table-column label="检测类型" property="defectType" />
            <el-table-column label="缺陷等级" property="defectLevel" />

            <el-table-column width="330" align="center" label="操作">
                <template #default="scope">

                    <el-button size="small" @click="handleEdit(scope.row)"><el-icon>
                            <EditPen />
                        </el-icon>编辑</el-button>
                    <el-popconfirm confirm-button-text="是" cancel-button-text="点错了" @confirm="handleDelete(scope.row)"
                        title="您是否要删除这行数据">
                        <template #reference>
                            <el-button size="small" type="danger"><el-icon>
                                    <Delete />
                                </el-icon>删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 表格 结束 -->
        
        <!-- 分页栏开始 -->
        <el-pagination style="margin-top: 15px;" :page-size="defectLimit" :pager-count="defectMaxPage"
            @current-change="changePage" background layout="prev, pager, next" :total="defectTotal" />
        <!-- 分页栏结束 -->

        <!-- 添加缺陷的弹窗窗体开始-->
        <el-dialog v-model="addDefectDialogVisible" title="添加缺陷" width="600" align-center>
            <!-- 添加缺陷的表单 -->
            <el-form label-width="100px" :model="newDefect" style="max-width: 460px">
                <el-form-item label="缺陷描述">
                    <el-input v-model="newDefect.defectName" />
                </el-form-item>

                <el-form-item label="检测类型">
                    <el-select v-model="newDefect.defectType" placeholder="请选择检测项类型">
                        <el-option label="尺寸" value="尺寸" />
                        <el-option label="外观" value="外观" />
                        <el-option label="重量" value="重量" />
                        <el-option label="性能" value="性能" />
                        <el-option label="成分" value="成分" />
                    </el-select>
                </el-form-item>

                <el-form-item label="缺陷等级">
                    <el-select v-model="newDefect.defectLevel" placeholder="请选择缺陷等级">
                        <el-option label="致命缺陷" value="致命缺陷" />
                        <el-option label="严重缺陷" value="严重缺陷" />
                        <el-option label="轻微缺陷" value="轻微缺陷" />
                    </el-select>
                </el-form-item>

            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancelAddDefect">取消</el-button>
                    <el-button type="primary" @click="doAddDefect">
                        提交
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 添加缺陷的弹窗窗体结束-->


        <!-- 编辑缺陷的抽屉弹窗开始 -->

        <el-dialog v-model="defectEditDrawer" title="编辑" width="600" align-center>
            <template #header>
                <h4>编辑缺陷</h4>
            </template>
            <template #default>
                <div>
                    <!-- 编辑缺陷的表单 -->
                    <el-form label-width="100px" :model="editRefect" style="max-width: 460px">
                        <el-form-item label="缺陷描述">
                            <el-input v-model="editRefect.defectName" />
                        </el-form-item>

                        <el-form-item label="检测类型">
                            <el-select v-model="editRefect.defectType" placeholder="请选择检测项类型">
                                <el-option label="尺寸" value="尺寸" />
                                <el-option label="外观" value="外观" />
                                <el-option label="重量" value="重量" />
                                <el-option label="性能" value="性能" />
                                <el-option label="成分" value="成分" />
                            </el-select>
                        </el-form-item>

                        <el-form-item label="缺陷等级">
                            <el-select v-model="editRefect.defectLevel" placeholder="请选择缺陷等级">
                                <el-option label="致命缺陷" value="致命缺陷" />
                                <el-option label="严重缺陷" value="严重缺陷" />
                                <el-option label="轻微缺陷" value="轻微缺陷" />
                            </el-select>
                        </el-form-item>

                    </el-form>
                </div>
            </template>
            <template #footer>
                <div style="flex: auto">
                    <el-button @click="cancelEdit">取消</el-button>
                    <el-button type="primary" @click="doUserEdit">提交</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 编辑用户的抽屉弹窗结束 -->


    </div>
</template>
<script>
import defectRequst from '../../../api/defect-api.js';



export default {
    data() {
        return {
            queryDefect: {},//查询缺陷的对象
            defects: [],//缺陷数据列表

          

           defectCurrPage: 1, // 分页的当前页码
           defectLimit: 10, //  分页的每页大小
           defectTotal: 0, //  列表的总条数
           defectMaxPage: 1,//  列表的最大页码

            addDefectDialogVisible: false,//新增缺陷窗口
            newDefect: {},//封装新增缺陷

            defectEditDrawer: false,//标记编辑缺陷窗口

            editRefect: {},// 当前正在编辑的缺陷对象
            
        }


    },
    methods: {

        //行删除事件
        handleDelete(row) {
            defectRequst.delete(row.defectId, res => {
                if (res.code == 200) {
                    this.$message({
                        type: "success",
                        message: "删除成功"
                    });
                    this.loaddefect();
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败"
                    });
                }
            })


        },
        //行提交编辑按钮
        doUserEdit() {
            // 提交editRefect即可
            defectRequst.edit(this.editRefect, res => {
                // 这里的res就是我们的后端返回的perResult对象
                if (res.code == '200') {
                    this.$message({
                        message: '编辑成功',
                        type: 'success'
                    });
                    //关闭窗体
                    this.defectEditDrawer = false;
                    // 重新加载数据
                    this.loaddefect();
                    // 将editRefect制空
                    this.editRefect = {};
                } else if (res.code == '300') {
                    this.$message({
                        message: '缺陷类型重复',
                        type: 'warning',
                    });
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error',
                    });
                }
            });


        },

        // 取消编辑操作
        cancelEdit() {

            // 将编辑的用户制空
            this.editRefect = {};
            // 关闭抽屉弹窗
            this.defectEditDrawer = false;
        },

        // 行编辑按钮
        handleEdit(row) {
            // 通过id加载要编辑的用户信息
            defectRequst.get(row.defectId, res => {
                // 将查询的用户信息设置到editUser中
                this.editRefect = res.data;
                // 显示抽屉弹窗
                this.defectEditDrawer = true;
            });
        },

        //新增按钮事件
        doAddDefect() {
            console.log(this);
            defectRequst.save(this.newDefect, res => {
                // 这里的res就是我们的后端返回的perResult对象
                if (res.code == '200') {
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    //关闭窗体
                    this.addDefectDialogVisible = false;
                    // 重新加载数据
                    this.loaddefect();
                    // 将newDefect制空
                    this.newDefect = {};
                } else if (res.code == '300') {
                    this.$message({
                        message: '缺陷描述重复',
                        type: 'warning',
                    });
                } else {
                    this.$message({
                        message: '添加失败',
                        type: 'error',
                    });
                }
            });
        },
        //取消添加按钮事件
        cancelAddDefect() {
            // 将newDefect制空
            this.newDefect = {};
            // 关闭窗体
            this.addDefectDialogVisible = false;
        },

        //工具栏-选中删除按钮事件
        handlerDeleteDefect() {
            // 获取表格对象
            let table = this.$refs.defectTable;
            // 通过API获取被选中的数据 
            let selectionRows = table.getSelectionRows();
            if (selectionRows == undefined || selectionRows.length == 0) {
                this.$message({
                    type: "info",
                    message: "您没有选中任何行"
                });
            }
            // console.log(selectionRows);
            // 可以进行批量改删除了
            //批量删除的第一件事
            //生产一个要删除的userId的数组
            let defectIds = [];

            //forEach 指的是从数组每一次取值是会赋值给user
            selectionRows.forEach(defect => {
                // console.log(defect);
                defectIds.push(defect.defectId);
            });
            console.log(defectIds);
            //调用API进行删除
            defectRequst.deletes(defectIds, res => {
                this.$message({
                    type: "success",
                    message: "成功删除" + res.msg + "条数据"
                });
                this.loadDicts();

            });

        },


        //工具栏-选中编辑按钮事件
        handlerEditDefect() {
            // 获取表格对象
            let table = this.$refs.defectTable;
            // 通过API获取被选中的数据 
            let selectionRows = table.getSelectionRows();
            if (selectionRows == undefined || selectionRows.length == 0) {
                this.$message({
                    type: "info",
                    message: "您没有选中任何行"
                });
            }
            // 获取第一行数据
            let row = selectionRows[0];
            // 调用行编辑事件
            this.handleEdit(row);
        },

        //缺陷列表的翻页函数
        changePage(value) {
            this.defectCurrPage = value;
            this.loaddefect();
        },

        // 加载缺陷数据
        loaddefect() {

            defectRequst.list(this.defectCurrPage, this.defectLimit,
                this.queryDefect, res =>  {
                // 设置数据
                this.defects = res.data.list;
                this.defectTotal = res.data.total;
                this.defectMaxPage = res.data.pages;
            });

        },
        //重置操作
        resettingdefect() {
            // 将newUser制空
            this.queryDefect = {};
            this.loaddefect();

        },
    }, mounted() {
        this.loaddefect();//加载缺陷数据
    }



}


</script>
<style></style>